import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/Layout/Index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'layout',
    redirect: '/user',
    meta: {
      title: '主页',
    },
    component: Layout
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login/Login.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: Layout,
    redirect: '/userlist',
    meta: {
      title: '基础管理',
      isMenu: true,
      isAuth: true,
      icon: 'iconfont icon-lingjuanzhongxin',
    },
    children: [
      {
        path: '/userlist',
        name: 'userlist',
        meta: {
          title: '用户管理',
          isAuth: true,
          icon: 'iconfont icon-yonghuguanli1',
          isMenu: true,
        },
        component: () => import('@/views/user/Index.vue')
      },
      {
        path: '/taglist',
        name: 'taglist',
        meta: {
          title: '标签管理',
          isAuth: true,
          icon: 'iconfont icon-youhuijuan',
          isMenu: true,
        },
        component: () => import('@/views/tag/Index.vue')
      }
    ]
  },
  {
    path: '/resource',
    name: 'resource',
    component: Layout,
    redirect: '/resourcelist',
    meta: {
      title: '资源管理',
      isMenu: true,
      isAuth: true,
      icon: 'iconfont icon-caidan',
    },
    children: [
      {
        path: '/resourcelist',
        name: 'resourcelist',
        meta: {
          title: '资源管理',
          isAuth: true,
          icon: 'iconfont icon-caidan',
          isMenu: true,
        },
        component: () => import('@/views/resource/Index.vue')
      },
    ]
  },
  {
    path: '*',
    name: 'all',
    redirect: '/404',
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404.vue'),
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (!sessionStorage.token) {
    if (to.path !== '/login') {
      next('/login')
      return;
    } else {
      next();
      return
    }
  } else {
    next();
  }
  next();
});

export default router
